package legumes.client.ui.otherboxes;

import legumes.client.evt.main.UserConnectedEvent;
import legumes.client.evt.main.UserConnectedEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEvent;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEventHandler;
import legumes.client.ui.util.wrapper.LatLngWrapper;
import legumes.client.ui.widgets.window.MaximizedStoreWindowMap;
import legumes.client.ui.widgets.window.StoreMapWindow;
import legumes.shared.domain.HomePlaceDelivery;
import legumes.shared.domain.PlaceDelivery;
import legumes.shared.domain.Store;

import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.geom.LatLng;
import com.smartgwt.client.widgets.events.MaximizeClickEvent;
import com.smartgwt.client.widgets.events.MaximizeClickHandler;

public class MapBoxPanel extends StoreMapWindow {
	
	private MaximizedStoreWindowMap maximizedWindowMap;
	private LatLng center;
	
	public MapBoxPanel(LatLng center_){
		super(-1, 200, center_);
		center = center_;
		setVisible(false);
		bind();
	}
	
	private void onPlaceDeliverySelectionChanged(PlaceDelivery pd_){
		if( ! isVisible() ){
			setVisible(true);
		}
		LatLng point = pd_ !=null? LatLngWrapper.converLatLng(pd_.getLatLng()):null;
		centerMapOn( point );
	}
	
	private void bind(){
		EventBusManager.getInstance().getEventBus().addHandler(
				PlaceDeliverySelectionChangedEvent.TYPE, 
				new PlaceDeliverySelectionChangedEventHandler(){

					@Override
					public void onStoreSelectionChanged(
							PlaceDeliverySelectionChangedEvent event) {
						onPlaceDeliverySelectionChanged( event.getPlaceDelivery() );
						getMapWidget().setZoomLevel(5);
					}
				});
		
		EventBusManager.getInstance().getEventBus().addHandler(
				UserConnectedEvent.TYPE, 
				new UserConnectedEventHandler(){

					@Override
					public void onUserConnected(UserConnectedEvent event) {
						onPlaceDeliverySelectionChanged( null );
					}
				});
	}
	
	@Override
	protected void initWindowProperties(){
		addMaximizeClickHandler(new MaximizeClickHandler() {
		
			@Override
			public void onMaximizeClick(MaximizeClickEvent event) {
				if(maximizedWindowMap==null){
					maximizedWindowMap = new MaximizedStoreWindowMap(center){
						
						@Override
						protected void onCloseAction(){
							super.onCloseAction();
							//Set the same zoom in the box panel
							MapBoxPanel.this.getMapWidget().setZoomLevel(
									maximizedWindowMap.getMapWidget().getBoundsZoomLevel(maximizedWindowMap.getMapWidget().getBounds()));
						}
						
					};
					
					
				}
				maximizedWindowMap.show();
				event.cancel();
			}
		});
	}
	
	@Override
	protected void initMapControls(){
		getMapWidget().addControl(new SmallMapControl());
		getMapWidget().setZoomLevel(5);
	}
	
	@Override
	protected void onStoreSelectionChanged(Store st_, PlaceDelivery pd_){
		if( pd_ instanceof HomePlaceDelivery ){
			drawHomePlaceDeliveryArea(pd_);
		}
	}
}